package com.example.function.project.controller;

import com.example.common.support.*;
import com.example.function.project.model.one.ProjectFile;
import com.example.function.project.model.pojo.ProjectFilePojo;
import com.example.function.project.requestBody.ProjectDeleteFileRequestBody;
import com.example.function.project.service.IProjectFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Description: <project模块controller >. <br>
 * <p>
 * <基本的crud>
 * </p>
 * generate time:2021-7-29 16:28:21
 *
 * @author hanqf
 * @version V1.0
 */

@RestController
@Slf4j
@RequestMapping("/proj")
public class ProjectFileController {


    @Resource(name = "projectFileService")
    protected IProjectFileService projectFileService;


    /**
     * 查询全部ProjectFile.
     *
     * @return AjaxResponse
     */
    @RequestMapping(value = "/projectFiles")
    @CP_OperateLog(value = "查询全部ProjectFile", type = CP_GlobalNamingConstant.OPERATE_LIST)
    public AjaxResponse projectFiles() {
        final List<ProjectFile> projectFileList = projectFileService.findAll();
        return AjaxResponse.success(ProjectFilePojo.wrapper(projectFileList));
    }

    /**
     * 按项目id查询全部ProjectFile.
     *
     * @return AjaxResponse
     */
    @RequestMapping(value = "/projectFiles/{projectId}")
    @CP_OperateLog(value = "查询全部ProjectFile", type = CP_GlobalNamingConstant.OPERATE_LIST)
    public AjaxResponse projectFiles(@PathVariable("projectId") Long projectId) {
        final List<ProjectFile> projectFileList = projectFileService.findProjectFilesByProjectId(projectId);
        return AjaxResponse.success(ProjectFilePojo.wrapper(projectFileList));
    }


    /**
     * <h2>上传项目文件</h2>
     * Created by hanqf on 2021/7/29 16:12.
     *
     * @author hanqf
     */
    @PostMapping("/file/upload/{projectId}")
    @CP_OperateLog(value = "上传项目文件", type = CP_GlobalNamingConstant.OPERATE_UPLOAD)
    public AjaxResponse addFile(@PathVariable("projectId") Long projectId, MultipartFile[] multipartFiles) {

        if (multipartFiles == null || multipartFiles.length == 0) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "未上传任何文件");
        }

        projectFileService.uploadProjectFiles(projectId, multipartFiles);

        final List<ProjectFile> files = projectFileService.findProjectFilesByProjectId(projectId);

        return AjaxResponse.success(ProjectFilePojo.wrapper(files));
    }


    /**
     * <h2>删除项目文件</h2>
     * Created by hanqf on 2021/7/29 16:12.
     *
     * @author hanqf
     */
    @PostMapping("/file/remove/{projectId}")
    @CP_OperateLog(value = "删除项目文件", type = CP_GlobalNamingConstant.OPERATE_DELETE)
    public AjaxResponse deleteFile(@PathVariable("projectId") Long projectId,@RequestBody ProjectDeleteFileRequestBody requestBody) {
        Long[] fileIds = requestBody.getFileIds();
        if (fileIds == null || fileIds.length == 0) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "未上传待删除的文件id");
        }

        projectFileService.delete(fileIds);

        final List<ProjectFile> files = projectFileService.findProjectFilesByProjectId(projectId);
        return AjaxResponse.success(ProjectFilePojo.wrapper(files));
    }




    /**
     * 获取下载文件连接.
     * 这种方式不好，无法修改文件名称
     * @return AjaxResponse
     */
    @RequestMapping(value = "/file/downloadUrl/{fileId}")
    @CP_OperateLog(value = "获取项目文件下载连接", type = CP_GlobalNamingConstant.OPERATE_DOWNLOAD)
    public AjaxResponse downloadUrlFile(@PathVariable("fileId") Long fileId) {
        final String fileDownUrl = projectFileService.getFileDownUrl(fileId);
        Map<String, Object> map = new HashMap<>();
        map.put("fileId", fileId);
        map.put("fileDownUrl", fileDownUrl);
        return AjaxResponse.success(map);
    }

    /**
     * 直接下载文件
     * 返回文件流
     */
    @RequestMapping(value = "/file/download/{fileId}")
    @CP_OperateLog(value = "下载项目文件", type = CP_GlobalNamingConstant.OPERATE_DOWNLOAD)
    public void downloadFile(@PathVariable("fileId") Long fileId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        projectFileService.downLoadFile(fileId, request, response);
    }


}

